package com.company.control;

import com.company.bean.UserInfoBean;
import com.company.model.UserModel;
import com.company.util.Myutils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "UserInfoAddServlet", value = "/UserInfoAddServlet")
public class UserInfoAddServlet extends HttpServlet {

    /**
     *
     * 表单数据的保存
     *  页面的跳转
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       //1.设置编码
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");

        //2.获取表单数据，并把表单数据保存到实例类中
        UserInfoBean userInfoBean = Myutils.getNewIntance(UserInfoBean.class);
        userInfoBean.setUserName(request.getParameter("userName"));
        userInfoBean.setUserPsw(request.getParameter("userPsw"));
        userInfoBean.setUserPhone(request.getParameter("userPhone"));
        //把从前台野蛮获取的表单数据的值转换为日期Date类型
       // userInfoBean.setUserRegiateDate(request.getParameter("userRegiateDate"));

        //3.使用model层进行数据保存的业务逻辑
        int i = Myutils.getNewIntance(UserModel.class).saveUserInfo(userInfoBean);


        //4.数据保存成功之后，跳转的保存结果显示页面
        if (i>0){
            request.getRequestDispatcher("WEB-INF/viewpages/userinfoB/UserInfoAddDetail.jsp").forward(request,response);
        }
        else {
            request.getRequestDispatcher("WEB-INF/viewpages/userinfoB/UserInfoAdd.jsp").forward(request,response);
        }
    }
}
